package com.isay.app.honey.ui.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.app.honey.ui.forget.ForgetActivity;
import com.isay.app.honey.ui.main.MainActivity;
import com.isay.app.honey.ui.regist.RegisterActivity;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.ToastUtils;

import org.json.JSONObject;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEdtName;
    private EditText mEdtPwd;
    private TextView mTvLogin;


    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, LoginActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        initView();
        initData(true);
    }


    @Override
    protected int getStatusBarColor() {
        return 0;
    }


    private void initView() {
        mEdtName = findViewById(R.id.edt_user_name);
        mEdtPwd = findViewById(R.id.edt_user_pwd);
        mTvLogin = findViewById(R.id.btn_login);
        mTvLogin.setOnClickListener(this);
        findViewById(R.id.tv_forget_pwd).setOnClickListener(this);
        findViewById(R.id.lay_no_account).setOnClickListener(this);
    }

    private void initData(boolean isLogin) {
        String username = UserManager.getUserName();
        mEdtName.setText(username);
        String password = UserManager.getUserPwd();
        mEdtPwd.setText(password);
        if (isLogin && !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            login();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_forget_pwd:
                ForgetActivity.launch(LoginActivity.this);
                break;
            case R.id.lay_no_account:
                RegisterActivity.launchForResult(LoginActivity.this);
                break;
            case R.id.btn_login:
                login();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            initData(false);
        }
    }

    private void login() {
        showLoading("登录中...");
        final String username = mEdtName.getText().toString().trim();
        final String password = mEdtPwd.getText().toString().trim();
        NetworkManager.login(username, password, new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                boolean result = loginSuccess(ret, msg, content);
                dismissDialog();
                if (result) {
                    UserManager.setUserName(username);
                    UserManager.setUserPwd(password);
                    MainActivity.launch(LoginActivity.this);
                    finish();
                } else {
                    ToastUtils.show(msg);
                }
            }

            @Override
            public void onFailure(Exception e) {
                dismissDialog();
                ToastUtils.show(e.getMessage());
            }
        });
    }

    public static boolean loginSuccess(int ret, String msg, String content) {
        try {
            JSONObject jsonObject = new JSONObject(content);
            String phpssid = jsonObject.optString("PHPSESSID");
            UserManager.setPHPSESSID(phpssid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ret == 200 && !TextUtils.isEmpty(UserManager.getPHPSESSID())) {
            return true;
        } else {
            return false;
        }
    }


}
